package com.tj.collection.controller.models.response;

import com.fqgj.common.api.ResponseData;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.user.dto.ImagesDto;
import com.kly.user.dto.Model;
import com.kly.user.dto.chatter.Chatter;
import com.tj.collection.db.domain.Admin;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ApiModel(value = "modelListRsp", description = "model列表")
public class ModelsListResponse implements ResponseData {

    @ApiModelProperty("model列表")
    private List<ModelVo> list;
    @ApiModelProperty("model列表总条数")
    private Integer totalCnt;


    public ModelsListResponse(List<Model> list,boolean showPrivate, String userPhotoPrefix, List<Admin> admins, Map<String, String> modelLikeRateMap) {
        List<ModelVo> lista = new ArrayList<>();
        list.forEach(model -> {
            ModelVo vo = new ModelVo();
            BeanUtils.copyProperties(model, vo);
            vo.setAvatarUrl(userPhotoPrefix + model.getAvatarUrl());
            vo.setImages(processImageUrls(userPhotoPrefix, model.getImages(), showPrivate));
            vo.setPrivateImages(processImageUrls(userPhotoPrefix, model.getPrivateImages(), showPrivate));
            vo.setPublicImages(processImageUrls(userPhotoPrefix, model.getPublicImages(), showPrivate));
            if (model.getAvatar() != null) {
                vo.setAvatar(ImagesDto.toDto(model.getAvatar().getImageCode(), model.getAvatar().getImageUrl()));
            }
            String adminAccount = null;
            if (CollectionUtils.isNotEmpty(admins)) {
                adminAccount = admins.parallelStream().filter(o -> o.getId().equals(model.getAdminId())).map(Admin::getAccount).findFirst().orElse("");
            }
            vo.setAdminAccount(adminAccount);
            if (modelLikeRateMap != null && modelLikeRateMap.containsKey(vo.getModelCode())) {
                vo.setLikeRate(modelLikeRateMap.get(vo.getModelCode()));
            } else {
                vo.setLikeRate("0.00%");
            }

            lista.add(vo);
        });

        this.list = lista;
    }

    private List<ImagesDto> processImageUrls(String userPhotoPrefix, List<ImagesDto> images, Boolean showPrivate) {
        if (CollectionUtils.isNotEmpty(images)) {
            images.forEach(o -> {
                if (showPrivate) {
                    String imageUrl = o.getImageUrl();
                    if(imageUrl.contains("images3") && !imageUrl.contains("video")){
                        imageUrl = imageUrl.replace("?","_734x1200?");
                    }
                    o.setImageUrl(userPhotoPrefix + imageUrl);
                } else {
                    o.setImageUrl("");
                }
            });
        }
        return images;
    }

    public Integer getTotalCnt() {
        return totalCnt;
    }

    public ModelsListResponse setTotalCnt(Integer totalCnt) {
        this.totalCnt = totalCnt;
        return this;
    }

    public List<ModelVo> getList() {
        return list;
    }

    public ModelsListResponse setList(List<ModelVo> list) {
        this.list = list;
        return this;
    }
}
